package com.avic.rong.fragment;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.avic.rong.MainActivity;
import com.avic.rong.MyApplication;
import com.avic.rong.R;
import com.avic.rong.activity.no.CriterionDetailActivity;
import com.avic.rong.activity.login.LoginActivity;
import com.avic.rong.activity.no.SearchIndexActivity;
import com.avic.rong.activity.no.SureOrderActivity;
import com.avic.rong.bean.CartListReloadEvent;
import com.avic.rong.bean.HomePageEvent;
import com.avic.rong.bean.MinePageEvent;
import com.example.minato.minastore.utils.AppUtils;
import com.github.lzyzsd.jsbridge.BridgeHandler;
import com.github.lzyzsd.jsbridge.BridgeWebView;
import com.github.lzyzsd.jsbridge.CallBackFunction;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;

public class FragmentCart extends Fragment {


    public static final String ARG_FROM_CAT = "arg_from_cat";

    @BindView(R.id.webview)
    BridgeWebView webView;


    public FragmentCart() {
    }

    public static FragmentCart newInstance(String param1, String param2) {
        FragmentCart fragment = new FragmentCart();
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView =  inflater.inflate(R.layout.fragment_fragment_cart, container, false);
        ButterKnife.bind(this,rootView);


        if (MyApplication.getInstance().userLoginBean != null){
            webView.loadUrl(AppUtils.BASE_URL + "app/cart/cartList?userIdSign=" + MyApplication.getInstance().userLoginBean.getUserId());
        }else{
            webView.loadUrl(AppUtils.BASE_URL + "app/cart/cartList");
        }


        //确认订单页面
        webView.registerHandler("pushVc", new BridgeHandler() {
            @Override
            public void handler(String data, CallBackFunction function) {

                data = data.replace("\\", "");
                JsonObject jsonObject = (JsonObject)new JsonParser().parse(data);
                String urlString = jsonObject.get("url").getAsString();
                if (urlString.contains("/sureOrder")){//去结算
                    startActivity(SureOrderActivity.queryIntent(getActivity(),urlString));
                }
            }
        });


        //登录
        webView.registerHandler("goLogin", new BridgeHandler() {
            @Override
            public void handler(String data, CallBackFunction function) {
                startActivity(LoginActivity.queryIntent(getActivity()));
            }
        });
//http://app1.rongrong.huazhi365.com/app/order/sureOrder?skuStr={98061:1,133632:1,93641:1,118627:1,128037:3,98065:2,113555:1}&orderType=4&userIdSign=212886
        //去购买
        webView.registerHandler("homeClick", new BridgeHandler() {
            @Override
            public void handler(String data, CallBackFunction function) {
//                ((MainActivity)getActivity()).switchFragment(R.id.rbtn_home);
//                function.onCallBack("submitFromWeb exe, response data from Java");
            }
        });


        //首页
        webView.registerHandler("homeClick", new BridgeHandler() {
            @Override
            public void handler(String data, CallBackFunction function) {
                EventBus.getDefault().post(new HomePageEvent());
            }
        });

        //搜索
        webView.registerHandler("searchClick", new BridgeHandler() {
            @Override
            public void handler(String data, CallBackFunction function) {
                startActivity(SearchIndexActivity.queryIntent(getActivity(), AppUtils.BASE_URL + "app/searchIndex",ARG_FROM_CAT));
            }
        });
        //我的融融
        webView.registerHandler("memberClick", new BridgeHandler() {
            @Override
            public void handler(String data, CallBackFunction function) {
                EventBus.getDefault().post(new MinePageEvent());
            }
        });

        //在线客服
        webView.registerHandler("pushVc", new BridgeHandler() {
            @Override
            public void handler(String data, CallBackFunction function) {
                data = data.replace("\\", "");
                JsonObject jsonObject = (JsonObject) new JsonParser().parse(data);
                String urlString = jsonObject.get("url").getAsString();
                if(MyApplication.getInstance().userLoginBean != null && !urlString.contains("userIdSign")){
                    if(urlString.contains("?")){
                        urlString = urlString + "&userIdSign=" + MyApplication.getInstance().userLoginBean.getUserId();
                    }else{
                        urlString = urlString + "?userIdSign=" + MyApplication.getInstance().userLoginBean.getUserId();
                    }
                }

                if (urlString.contains("/cservice")){//在线客服
                    String tempUrl = urlString.replace("userIdSign","userId");
                    Intent intent = new Intent();
                    intent.setData(Uri.parse(tempUrl));
                    intent.setAction(Intent.ACTION_VIEW);
                    startActivity(intent);
//                    startActivity(IMActivity.queryIntent(getActivity(),urlString.replace("userIdSign","userId")));
                }else if(urlString.contains("/good/info")){//商品详情
                    startActivity(CriterionDetailActivity.queryIntent(getActivity(),urlString,ARG_FROM_CAT));
                }else if(urlString.contains("/sureOrder")){
                    startActivity(SureOrderActivity.queryIntent(getActivity(),urlString));
                }
            }
        });
        return rootView;
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void reloadUrl(CartListReloadEvent cartListReloadEvent){
        if (webView != null) {
            if (MyApplication.getInstance().userLoginBean != null) {
                webView.loadUrl(AppUtils.BASE_URL + "app/cart/cartList?userIdSign=" + MyApplication.getInstance().userLoginBean.getUserId());
            } else {
                webView.loadUrl(AppUtils.BASE_URL + "app/cart/cartList");
            }
        }
    }


    @Override
    public void onStart() {
        super.onStart();
        EventBus.getDefault().register(this);
    }

    @Override
    public void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
        EventBus.getDefault().removeAllStickyEvents();
    }
}
